/*
 *
 */

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails", //the full name of the class to create
        jsx3.lang.Object, //  extends jsx3.lang.Object
        null, // does not implements any interfaces
        function (shdResDetails) {

            shdResDetails.ONLOADEDIT = "tibco.admin.enterpriseAssets.shdResDefs.LOADEDIT";
            shdResDetails.ONSTARTEDIT = "tibco.admin.enterpriseAssets.shdResDefs.STARTEDIT";
            shdResDetails.ONCANCEL = "tibco.admin.enterpriseAssets.shdResDefs.CANCEL";
            shdResDetails.ONSAVE = "tibco.admin.enterpriseAssets.shdResDefs.SAVEDETAILS";

            shdResDetails.prototype.master = null;
            shdResDetails.prototype.parent = null;
            shdResDetails.prototype.id = null;
            shdResDetails.prototype.name = null;
            shdResDetails.prototype.description = null;
            shdResDetails.prototype.mainComponent = null;

            shdResDetails.prototype.roComponentList = null;
            shdResDetails.prototype.rwComponentList = null;
            shdResDetails.prototype.editableComponentList = null;

            shdResDetails.prototype.editMode = null;

            shdResDetails.prototype.init = function(master, parent, id, name, description, handler) {
                this.jsxsuper();
                this.master = master;
                this.parent = parent;
                this.handler = handler
                this.id = id;
                this.name = name ? name : this.getDefaultName();
                this.description = description ? description : this.getDefaultDescription();
                this.editableComponentList = new jsx3.util.List(["txtName_25", "txtDescription_25"]);
                this.rwComponentList = new jsx3.util.List(["btnSaveDetails_25", "btnCancelDetails_25"]);
                this.rwComponentList.addAll(this.editableComponentList, this.rwComponentList.size());
                this.roComponentList = new jsx3.util.List(["btnEditDetails_25", "lblcName_25", "lblcDescription_25"]);
            }

            shdResDetails.prototype.getType = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.getDetailsOperation = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.getDetailsOperationRules = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.editOperation = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.editOperationRules = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.addOperation = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.addOperationRules = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.getDetailsComponent = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.getSecondaryDetailsComponent = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.getDefaultName = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.getDefaultDescription = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.setEditRequestDetails = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.setAddRequestDetails = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.getHelpId = jsx3.lang.Method.newAbstract();
            shdResDetails.prototype.setHelpId = function(hid) {
                this.mainComponent.getDescendantOfName("btnHelp_25").helpid = hid;
            }

            shdResDetails.prototype.loadView = function() {
                this.editMode = "view";
                this.loadDetails();
            }

            shdResDetails.prototype.loadEdit = function() {
                this.editMode = "edit";
                this.switchMode();
                this.setEditable(true);
                this.mainComponent.getDescendantOfName("txtName_25").setEnabled(jsx3.gui.Form.STATEDISABLED, true)//070929
            }

            shdResDetails.prototype.loadDetails = function() {
                tibco.admin.Main.loadUIResourceAsync(this.parent, "tibco.admin",
                        "tibco/admin/enterpriseAssets/shdResDefs/components/shdResDefDetails.xml",
                        this, this.initializeGenericDetails);
            }

            shdResDetails.prototype.initializeGenericDetails = function(objEvent) {
                if (objEvent.status == "SUCCESS") {
                    this.mainComponent = objEvent.obj;
                    if (this.editMode == "view") {
                        this.mainComponent.getDescendantOfName("lblcName_25").setText(this.name, true);
                        this.mainComponent.getDescendantOfName("lblcDescription_25").setText(this.description, true);
                    }
                    else {
                        this.mainComponent.getDescendantOfName("txtName_25").setValue(this.name, true);
                        this.mainComponent.getDescendantOfName("txtDescription_25").setValue(this.description, true);
                    }
                    this.mainComponent.getDescendantOfName("lblcType_25").setText(this.getType(), true);
                    this.setHelpId(this.getHelpId());

                    tibco.admin.Main.loadUIResourceAsync(this.mainComponent.getDescendantOfName("paneDetailsPrimary_25"),
                            "tibco.admin", this.getDetailsComponent(), this, this.initializeSpecificDetails);
                }
                this.initializeGenericParam();
            }

            shdResDetails.prototype.initializeSpecificDetails = function(objEvent) {
                if (objEvent.status == "SUCCESS") {
                    var me = this;
                    this.mainComponent.getDescendantOfName("btnCancelDetails_25").subscribe(shdResDetails.ONCANCEL, me,
                            me.onCancelEdit);
                    this.mainComponent.getDescendantOfName("btnSaveDetails_25").subscribe(shdResDetails.ONSAVE, me,
                            me.onSave);
                    this.mainComponent.getDescendantOfName("btnEditDetails_25").subscribe(shdResDetails.ONLOADEDIT, me,
                            me.loadEdit);

                    jsx3.util.Logger.GLOBAL.debug("Loading secondary details.");
                    if (this.getSecondaryDetailsComponent()) {
                        tibco.admin.Main.loadUIResourceAsync(this.mainComponent.getDescendantOfName("paneDetailsSecondary_25"),
                                "tibco.admin", this.getSecondaryDetailsComponent(), me, me.initializeSecondaryDetails);
                    }
                    else {
                        this.callGetConfigDetails();
                    }
                }
            }

            shdResDetails.prototype.initializeSecondaryDetails = function(objEvent) {
                if (objEvent.status == "SUCCESS") {
                    // TODO: set width of primary area to less than 100%!
                    this.mainComponent.getDescendantOfName("spltDetails_25").setSubcontainer1Pct(50, true);
                    this.callGetConfigDetails();
                }
                else {
                    jsx3.util.Logger.GLOBAL.error("Couldn't load secondary details:" + objEvent.status);
                }
            }

            shdResDetails.prototype.loadAdd = function() {
                this.editMode = "add";
                tibco.admin.Main.loadUIResourceAsync(this.parent, "tibco.admin",
                        "tibco/admin/enterpriseAssets/shdResDefs/components/shdResDefDetails.xml",
                        this, this.initializeGenericAdd);
            }

            shdResDetails.prototype.initializeGenericAdd = function(objEvent) {
                if (objEvent.status == "SUCCESS") {
                    this.mainComponent = objEvent.obj;
                    this.mainComponent.getDescendantOfName("txtName_25").setValue(this.name, true);
                    this.mainComponent.getDescendantOfName("lblcType_25").setText(this.getType(), true);
                    this.mainComponent.getDescendantOfName("txtDescription_25").setValue(this.description, true);
                    tibco.admin.Main.loadUIResourceAsync(this.mainComponent.getDescendantOfName("paneDetailsPrimary_25"),
                            "tibco.admin", this.getDetailsComponent(), this, this.initializeSpecificAdd);
                }
                this.initializeGenericParam();
            }

            shdResDetails.prototype.initializeSpecificAdd = function(objEvent) {
                if (objEvent.status == "SUCCESS") {
                    var me = this;
                    this.mainComponent.getDescendantOfName("btnCancelDetails_25").subscribe(shdResDetails.ONCANCEL, me,
                            me.onCancelEdit);
                    this.mainComponent.getDescendantOfName("btnSaveDetails_25").subscribe(shdResDetails.ONSAVE, me,
                            me.onSave);
                    this.mainComponent.getDescendantOfName("btnEditDetails_25").subscribe(shdResDetails.ONLOADEDIT, me,
                            me.loadEdit);

                    if (this.getSecondaryDetailsComponent()) {
                        tibco.admin.Main.loadUIResourceAsync(this.mainComponent.getDescendantOfName("paneDetailsSecondary_25"),
                                "tibco.admin", this.getSecondaryDetailsComponent(), me, me.initializeSecondaryAdd);
                    }
                    else {
                        this.switchMode();
                        this.setEditable(true);
                    }
                }
            }

            shdResDetails.prototype.initializeSecondaryAdd = function(objEvent) {
                if (objEvent.status == "SUCCESS") {
                    this.mainComponent.getDescendantOfName("spltDetails_25").setSubcontainer1Pct(50, true);
                    this.switchMode();
                    this.setEditable(true);
                }
                else {
                    jsx3.util.Logger.GLOBAL.error("Couldn't load secondary details:" + objEvent.status);
                }
            }

            shdResDetails.prototype.switchMode = function() {
                if ((this.editMode == "view")
                        && (this.mainComponent.getDescendantOfName("btnCancelDetails_25").getDisplay()
                        == jsx3.gui.Block.DISPLAYNONE))
                    return;
                var roDisplay = (this.editMode == "view") ? jsx3.gui.Block.DISPLAYBLOCK : jsx3.gui.Block.DISPLAYNONE;
                var rwDisplay = (this.editMode == "view") ? jsx3.gui.Block.DISPLAYNONE : jsx3.gui.Block.DISPLAYBLOCK;

                jsx3.util.Logger.GLOBAL.debug("switching mode for " + this.mainComponent);
                var iter = this.roComponentList.iterator();
                while (iter.hasNext()) {
                    var foo = iter.next();
                    this.mainComponent.getDescendantOfName(foo).setDisplay(roDisplay, true);
                }
                iter = this.rwComponentList.iterator();
                while (iter.hasNext()) {
                    var foo = iter.next();
                    this.mainComponent.getDescendantOfName(foo).setDisplay(rwDisplay, true);
                }
            }

            shdResDetails.prototype.setEditable = function(state) {
                var editEnabled = state ? jsx3.gui.Form.STATEENABLED : jsx3.gui.Form.STATEDISABLED;
                var editVisible = state ? jsx3.gui.Block.VISIBILITYVISIBLE : jsx3.gui.Block.VISIBILITYHIDDEN;
                if (this.editMode != "view") {
                    var iter = this.editableComponentList.iterator();
                    while (iter.hasNext()) {
                        var foo = iter.next();
						if(foo.substring(0,8) == "lblSvars"){
							var relateSvarTxt = this.mainComponent.getDescendantOfName(foo.substring(8,foo.length));
							if(relateSvarTxt.sVarName != null && relateSvarTxt.sVarName != "" && relateSvarTxt.sVarName != undefined){
								this.mainComponent.getDescendantOfName(foo).setVisibility(editVisible, true);
							}
						}else{
							this.mainComponent.getDescendantOfName(foo).setEnabled(editEnabled, true);
						}
                        if (state) {
                            // TODO: make this "perfect"? Worth the overhead?
                        }
                    }
                    if (state) {
                        this.onStartEdit();
                    }
                }
                else {
                    this.mainComponent.getDescendantOfName("btnEditDetails_25").setEnabled(editEnabled, true);
                }
            }

            shdResDetails.prototype.onStartEdit = function(objEvent) {
                this.setHelpId(this.getHelpId());
                var saveButton = this.mainComponent.getDescendantOfName("btnSaveDetails_25");
                if (saveButton.getEnabled() == jsx3.gui.Form.STATEDISABLED) {
                    saveButton.setEnabled(jsx3.gui.Form.STATEENABLED, true);
                }
                this.handler.beginEditMode();
            }

            shdResDetails.prototype.onCancelEdit = function(objEvent) {
                if (this.editMode != "add") {
                    this.editMode = "view";
                    this.switchMode();
                    this.callGetConfigDetails();
                }
                else { // add
                    this.parent.removeChildren();
                }
                this.handler.endEditMode();
            }

            shdResDetails.prototype.onSave = function(objEvent) {
                jsx3.util.Logger.GLOBAL.debug("On save: " + this.editMode);

                if (this.editMode == "edit") {
                    this.callEditResource(objEvent);
                }
                else { // add
                    this.callAddResource(objEvent);
                }

            }

            // WSDL operations
            // TODO: error handling
            // WSDL operation - getXxxxResourceDetails
            shdResDetails.prototype.callGetConfigDetails = function() {
                if(!this.id) {
                    return;
                }
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        this.getDetailsOperationRules(), this.getDetailsOperation(),
                        "Get " + this.getType() + " Shared Resources Configuration Details");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());

                var doc = jsx3.xml.CDF.Document.newDocument();
                doc.getRecordNode("jsxroot").setAttribute("EntityID", this.id);
                doc.getRecordNode("jsxroot").setAttribute("name", this.name);
                tibco.admin.Main.getServer().Cache.setDocument("xmlCacheIDGetDetailsRequest_25", doc);
                //subscribe and call
                var me = this;
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetConfigDetailsSuccess);
                objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.ongetConfigDetailsError);
                objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.ongetConfigDetailsInvalid);
                objService.doCall();
            }

            shdResDetails.prototype.ongetConfigDetailsSuccess = function(response) {
                // TODO: double check entity ID?
                this.name = response.getAttribute("name");
                this.mainComponent.getDescendantOfName("lblcName_25").setText(this.name, true);
                this.mainComponent.getDescendantOfName("txtName_25").setValue(this.name, true);
                this.description = response.getAttribute("description");
                this.mainComponent.getDescendantOfName("lblcDescription_25").setText(this.description ? this.description : "", true);
                this.mainComponent.getDescendantOfName("txtDescription_25").setValue(this.description, true);
                this.valiName(this.mainComponent.getDescendantOfName("txtName_25"));
            }

            // TODO: check usage for log functions
            shdResDetails.prototype.ongetConfigDetailsError = function(objEvent) {
            }

            shdResDetails.prototype.ongetConfigDetailsInvalid = function(objEvent) {
            }

            // WSDL operation - editXxxxResourceDetails
            shdResDetails.prototype.callEditResource = function() {
                this.setEditable(false);

                var objService = tibco.admin.Main.loadService("tibco.admin", this.editOperationRules(),
                        this.editOperation(), "Edit " + this.getType() + " Shared Resource");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());

                objService.EntityID = this.id;
                this.name = this.mainComponent.getDescendantOfName("txtName_25").getValue();
                objService.name = this.name;
                this.description = this.mainComponent.getDescendantOfName("txtDescription_25").getValue();
                objService.description = this.description;
                this.setEditRequestDetails(objService);

                //subscribe and call
                var me = this;
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onCallEditResourceSuccess);
                objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onCallEditResourceError);
                objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onCallEditResourceInvalid);
                objService.doCall();
            }

            shdResDetails.prototype.onCallEditResourceSuccess = function(objEvent) {
                this.handler.endEditMode();
                jsx3.util.Logger.GLOBAL.debug('shdResDefs - Edit successful');
                this.editMode = "view";
                this.switchMode();
                this.callGetConfigDetails();
                this.master.refreshList(this.id);
            }

            // TODO: check usage for log functions
            shdResDetails.prototype.onCallEditResourceError = function(objEvent) {
                this.setEditable(true);
            }

            shdResDetails.prototype.onCallEditResourceInvalid = function(objEvent) {
                this.setEditable(true);
            }

            // WSDL operation - addXxxxResourceDetails
            shdResDetails.prototype.callAddResource = function() {
                this.setEditable(false);

                var objService = tibco.admin.Main.loadService("tibco.admin", this.addOperationRules(),
                        this.addOperation(), "Add " + this.getType() + " Shared Resource");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());

                this.name = this.mainComponent.getDescendantOfName("txtName_25").getValue();
                objService.name = this.name;
                this.description = this.mainComponent.getDescendantOfName("txtDescription_25").getValue();
                objService.description = this.description;
                this.setAddRequestDetails(objService);

                //subscribe and call
                var me = this;
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onCallAddResourceSuccess);
                objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onCallAddResourceError);
                objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onCallAddResourceInvalid);
                objService.doCall();
            }

            shdResDetails.prototype.onCallAddResourceSuccess = function(objEvent) {
                this.handler.endEditMode();
                // TODO: set entity ID
                this.id = objEvent.target.EntityID;
                jsx3.util.Logger.GLOBAL.debug("Add successful");
                this.editMode = "view";
                this.switchMode();
                // TODO: reload matrix and select?
                this.master.refreshList(this.id);
            }

            // TODO: check usage for log functions
            shdResDetails.prototype.onCallAddResourceError = function(objEvent) {
                this.setEditable(true);
            }

            shdResDetails.prototype.onCallAddResourceInvalid = function(objEvent) {
                this.setEditable(true);
            }
            // End WSDL operations



            /*070818
           * if user doesn't fishish all the required fields, the "save" button will be disabled.  part1
           */
            shdResDetails.prototype.passedItems = {};

            shdResDetails.prototype.initializeGenericParam = function() {
                this.mainComponent.handler = this;
                this.passedItems = {};
                this.passedItems["namePassed"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtName_25").getValue())?true:false;
            }

            shdResDetails.prototype.valiName = function(obj, strValue) {
                this.passedItems["namePassed"] = tibco.admin.util.Validation.doValidateNotEmpty(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@oName_25"));
                this.enableSaveBtn4Add();
            }

            shdResDetails.prototype.enableSaveBtn4Add = function() {
                var allPassed = true;
                for (var item in this.passedItems) {
                    if (!this.passedItems[item]) allPassed = false;
                }
                if (allPassed) this.mainComponent.getDescendantOfName("btnSaveDetails_25").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                else this.mainComponent.getDescendantOfName("btnSaveDetails_25").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
            }

        }
        );